<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        $banner = Db::name('banner')->where([

        ])->select();
        $this->assign('banner', $banner);

        $nums = Db::name('nums')->select();
        $this->assign('nums', $nums);

        $about = Db::name('about')->select();
        $this->assign('about', $about);

        $product_cate = Db::name('category')->where([
            'pid' => 0
        ])->order('weigh desc,id desc')->select();
        $this->assign('product_cate', $product_cate);

        $product = Db::name('product')->where([
            'recmd_switch' => 1
        ])->order('weigh desc,id desc')->limit(0,8)->select();
        $this->assign('product', $product);

        $plan = Db::name('plan')->where([
            'recmd_switch' => 1
        ])->order('weigh desc,id desc')->limit(0,8)->select();
        $this->assign('plan', $plan);

        $news = Db::name('news')->where([
            'recmd_switch' => 1
        ])->order('weigh desc,id desc')->limit(0,2)->select();
        $this->assign('news', $news);

        $cases = Db::name('cases')->where([
            'recmd_switch' => 1
        ])->order('weigh desc,id desc')->limit(0,4)->select();
        $this->assign('cases', $cases);

        $lanmu_map = Db::name('lanmu')->select();
        $_lanmu_map = [];
        foreach ($lanmu_map as $k => $v) {
            $_lanmu_map[$v['id']] = $v;
        }
        $this->assign('lanmu_map', $_lanmu_map);

        return $this->view->fetch();
    }

    public function submit()
    {
        $param = $this->request->post();
        Db::name('message')->insert([
            'username' => $param['lc_name'],
            'tel' => $param['lc_tel'],
            'email' => $param['lc_email'],
            'content' => $param['lc_content'],
        ]);
        return $this->view->fetch();
    }

    public function search()
    {
        $param = $this->request->post();
        $keyword = $param['key'];
        $news = Db::name('news')->where([
            'title' => ['like', '%' . $keyword . '%'],
        ])->select();
        $this->assign('news', $news);

        $product = Db::name('product')->where([
            'title' => ['like', '%' . $keyword . '%'],
        ])->select();
        $this->assign('product', $product);

        $cases = Db::name('cases')->where([
            'title' => ['like', '%' . $keyword . '%'],
        ])->select();
        $this->assign('cases', $cases);

        $plan = Db::name('plan')->where([
            'title' => ['like', '%' . $keyword . '%'],
        ])->select();
        $this->assign('plan', $plan);

        return $this->view->fetch();
    }

}
